package com.baiwei.vpnapi.controller;

import com.baiwei.vpnapi.entity.reqentity.ApplePayChargeInput;
import com.baiwei.vpnapi.entity.reqentity.PaymentInput;
import com.baiwei.vpnapi.service.PayService;
import com.baiwei.vpnapi.utils.ResultVoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: vpnapi
 * @description: 支付相关控制器
 * @author: Mr. Stone
 * @create: 2020-03-06 16:08
 **/

@Controller
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private PayService paymentService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
    * @Description: 创建订单，发起支付
    * @return: 拉起支付的表单页面
    * @Author: Mr.Stone
    * @Date: 2020/3/14
    */
    @PostMapping("/commit")
    public String commitOrder(@RequestBody PaymentInput input, Model model) {
        return paymentService.commitOrder(input, model);
    }

//    @PostMapping("/commitMpay")
//    public String commitMpay(@RequestBody PaymentInput input, Model model) {
//        return paymentService.commitMpayOrder(input, model);
//    }

    /**
    * @Description: 第三方支付paydollar支付结果回调
    * @return: ok
    * @Author: Mr.Stone
    * @Date: 2020/3/14
    */
    @PostMapping("/success")
    public String successCharge() {
        return paymentService.successCharge();
    }

    /**
    * @Description: mPay支付结果回调
    * @Param:
    * @return:
    * @Author: Mr.Stone
    * @Date: 2020/5/27
    */
//    @PostMapping("/mpaysuccess")
//    public String mpaysuccessCharge() {
//        return paymentService.mpaySuccessCharage();
//    }

    /**
    * @Description: 苹果票据校验
    * @Param: 
    * @return: 
    * @Author: Mr.Stone
    * @Date: 2020/3/14
    */
    @PostMapping("/ioscharge")
    @ResponseBody
    public Object iosCharge(@RequestBody ApplePayChargeInput input) {
        input.validation();
        return ResultVoUtils.success(paymentService.iosPay(input.getTransactionId(), input.getPayload()));
    }

    @PostMapping("/commitWeb")
    public String commitOrderForWeb(@RequestParam String payMethod, @RequestParam String packageId, @RequestParam String token, Model model) {
        return paymentService.commitOrderForWeb(payMethod, packageId,token,model);
    }



    @PostMapping("/vipbuy")
    @ResponseBody
    public Object vipPay(@RequestBody Map<String, String> info) {
        if(info.get("packageId") == null || info.get("packageId").equals("")){
            throw new RuntimeException("packageId empty");
        }
        if(info.get("username") == null || info.get("username").equals("")) {
            throw new RuntimeException("username empty");
        }
        if(info.get("token") == null || info.get("token").equals("")) {
            throw new RuntimeException("token empty");
        }
        String adminToken = stringRedisTemplate.opsForValue().get("adminToken");
        if(!info.get("token").equals(adminToken)) {
            throw new RuntimeException("token invalid");
        }
        return ResultVoUtils.success(paymentService.vipBuy(info.get("packageId"), info.get("username")));
    }

}
